<?php
namespace Admin\Controller;
use Think\Controller;
class TypeController extends Controller {

	//新增公众号类别
	public function TypeAdd(){
		$typeName = I('post.single');
		if(!empty($typeName)){
			$data['type_name'] = $typeName;

			$arr = M('public_type')->select();
	        foreach($arr as $v){
	            $rank = $v['id']+1;
	        }
       		$data['rank']  = $rank;

			if(M('public_type')->add($data)){
				$ajaxReturn['code'] = '1';
				$ajaxReturn['msg']  = 'SUCCESS';
				$ajaxReturn['data'] = '添加成功';
				$this->ajaxReturn($ajaxReturn);
			}else{
				$ajaxReturn['code'] = '2';
    			$ajaxReturn['msg']  = 'FAIL';
    			$ajaxReturn['data'] = '公众号类型添加失败';
    			$this->ajaxReturn($ajaxReturn);
			}
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '公众号类型不能为空';
			$this->ajaxReturn($ajaxReturn);
		}
	}



	//公众号类别展示
	public function TypeShow(){
		if($_SERVER['REQUEST_METHOD'] == "GET"){

			$public_type = M('public_type');
	        //查询数据总条数
	        $number      = $public_type->field('type_id,rank,type_name')->count();

	        //设置每页条数
	        $page_size = I('get.page_size');
	        //计算可以分为多少页
	        $page_count = ceil($number/$page_size);
	        //接收当前页
	        $page     =  I('get.page');
	        
	        $start    = ($page-1)*$page_size;

			$type = $public_type->field('type_id,rank,type_name')->order('rank desc')->limit($start,$page_size)->select();

			if($type){
				$data['page']['total_page'] = $page_count;
            	$data['list'] = $type;

	            $ajaxReturn['code'] = 1;
	            $ajaxReturn['msg'] = "SUCCESS";
	            $ajaxReturn['data'] = $data;
				$this->ajaxReturn($ajaxReturn);
			}
		}
	}


	//展示修改公众号类别页面
	public function updateType(){
		$type_id = I('get.type_id');		//公众号类型id
		if(!empty($type_id)){
			$update = M('public_type')->where("type_id=$type_id")->field('type_id,type_name')->find();
			if($update){				//如果成功返回200，如果是失败返回10010
				$ajaxReturn['code'] = '1';
				$ajaxReturn['msg']  = 'SUCCESS';
				$ajaxReturn['data'] = $update;
				$this->ajaxReturn($ajaxReturn);
			}
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '公众号类别id不能为空';
			$this->ajaxReturn($ajaxReturn);
		}
	}



	//修改公众号类别
	public function updateType_pro(){
		$type_id = I('post.id');		//公众号类型id
		$typeName = I('post.single');		//公众号类别名称	
		if(!empty($typeName)){
			$data['type_name'] = $typeName;
			$update_type = M('public_type')->where("type_id=$type_id")->save($data);
			if($update_type){
				$ajaxReturn['code'] = '1';
				$ajaxReturn['msg']  = '公众号类别修改成功';
				$this->ajaxReturn($ajaxReturn);
			}
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '公众号类别名称不能为空';
			$this->ajaxReturn($ajaxReturn);
		}
	}



	//删除公众号类别
	public function delete(){
		$type_id = I('get.id');		//公众号类型id
		if(!empty($type_id)){
			$delete_type = M('public_type')->where("type_id=$type_id")->delete();	//删除公众号分类
			if($delete_type){
				$wechart_public  = M('wechart_public')->where("type_id=$type_id")->delete(); 	//删除该分类下的公众号
				if($wechart_public){
					$ajaxReturn['code'] = '1';
					$ajaxReturn['msg']  = '公众号类别删除成功';
					$this->ajaxReturn($ajaxReturn);
				}else{
					$ajaxReturn['code'] = '2';
					$ajaxReturn['data'] = '删除失败';
					$this->ajaxReturn($ajaxReturn);
				}
			}else{
				$ajaxReturn['code'] = '2';
				$ajaxReturn['msg']  = 'FAIL';
				$ajaxReturn['data'] = '删除失败';
				$this->ajaxReturn($ajaxReturn);
			}
		}
	}


	/*
     * 排序
     */
    public function rank (){

        $thisid = I('post.id');

        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $ArticleModel = M('public_type');

        $thisinfo   = $ArticleModel->where('type_id='.$thisid)->getField('rank,type_name');
        $thisrank   = array_keys($thisinfo)[0];

        $nextinfo   = $ArticleModel->where('rank'.$and.$thisrank)->order('rank '.$order)->getField('type_id,rank');
        
        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        $ajaxReturn['data'] = $nextinfo;

        if($thisrank && $nextid && $nextrank){

            $ArticleModel-> where('type_id='.$thisid)->setField('rank',$nextrank);
            $ArticleModel-> where('type_id='.$nextid)->setField('rank',$thisrank);

            $data['thisid'] = $thisid;
            $data['thisrank'] = $thisrank;
            $data['nextid'] = $nextid;
            $data['nextrank'] = $nextrank;

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }
}